package com.rdc.jiajiadictionary.dailysentence;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.rdc.jiajiadictionary.R;


///**
// * A simple {@link Fragment} subclass.
// * Use the {@link DailySentenceFragment#newInstance} factory method to
// * create an instance of this fragment.
// */
public class DailySentenceFragment extends Fragment implements DailySentenceViewInterface, View.OnClickListener {

//    // TODO: Rename parameter arguments, choose names that match
//    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
//    private static final String ARG_PARAM1 = "param1";
//    private static final String ARG_PARAM2 = "param2";
//
//    // TODO: Rename and change types of parameters
//    private String mParam1;
//    private String mParam2;
//
//    /**
//     * Use this factory method to create a new instance of
//     * this fragment using the provided parameters.
//     *
//     * @param param1 Parameter 1.
//     * @param param2 Parameter 2.
//     * @return A new instance of fragment DailySentenceFragment.
//     */
//    // TODO: Rename and change types and number of parameters
//    public static DailySentenceFragment newInstance(String param1, String param2) {
//        DailySentenceFragment fragment = new DailySentenceFragment();
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
//        return fragment;
//    }
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            mParam1 = getArguments().getString(ARG_PARAM1);
//            mParam2 = getArguments().getString(ARG_PARAM2);
//        }
//    }

//    public DailySentenceFragment() {
//        // Required empty public constructor
//    }

//    private TextView chineseTv;
//    private TextView englishTv;
    private TextView loadTv;
    private RelativeLayout photoRl;
    private ImageView backIv;
    private ImageView playIv;

    private DailySentencePresenter mDailySentencePresenter;


    /**
     * 重写Fragment的onResume方法，在onResume的同时进行网络判断
     * 有网则调用presenter的start方法进行每日一句的展示
     */
    @Override
    public void onResume() {
        super.onResume();
        ConnectivityManager connectionManager = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            mDailySentencePresenter.start();
        } else {
            loadTv.setText("当前网络不可用...");
            backIv.setBackgroundResource(R.drawable.close_black);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.daily_sentence_fragment
                ,container,false);
//        chineseTv = (TextView) view.findViewById(R.id.tv_chinese);
//        englishTv = (TextView) view.findViewById(R.id.tv_english);
        photoRl = (RelativeLayout) view.findViewById(R.id.rl_dailySentencePhoto);
        backIv = (ImageView) view.findViewById(R.id.iv_close);
        playIv = (ImageView) view.findViewById(R.id.iv_playSentence);
        loadTv = (TextView) view.findViewById(R.id.tv_load);
        backIv.setOnClickListener(this);
        playIv.setOnClickListener(this);
        return view;
    }

    /**
     * 提供更新每日一句图片的方法给presenter层调用
     * @param drawable
     */
    @Override
    public void setData(Drawable drawable) {


        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                chineseTv.setText(chinese);
//                englishTv.setText(english);
                photoRl.setBackground(drawable);
                loadTv.setVisibility(View.GONE);

            }
        });

    }

    public void toast(String text){
        Toast.makeText(getContext (), text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 绑定view层的presenter
     * @param presenter
     */
    @Override
    public void setPresenter(DailySentencePresenter presenter) {
        mDailySentencePresenter = presenter;
    }

    /**
     * 监听点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_close:
                getActivity().finish();
                break;
            case R.id.iv_playSentence:
                mDailySentencePresenter.play();
        }
    }
}